package com.ruoyi.common.utils.sign;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Md5加密方法
 * 
 * @author ruoyi
 */
public class Md5Utils
{
    private static final Logger log = LoggerFactory.getLogger(Md5Utils.class);

    public static String md5New(String str) {
        char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            byte[] bytes = str.getBytes();
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(bytes);
            byte[] updateBytes = messageDigest.digest();
            int len = updateBytes.length;
            char[] myChar = new char[len * 2];
            int k = 0;
            for (byte byte0 : updateBytes) {
                myChar[k++] = hexDigits[byte0 >>> 4 & 0x0f];
                myChar[k++] = hexDigits[byte0 & 0x0f];
            }
            return new String(myChar);
        } catch (Exception e) {
            return null;
        }
    }

    private static byte[] md5(String s)
    {
        MessageDigest algorithm;
        try
        {
            algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(s.getBytes("UTF-8"));
            byte[] messageDigest = algorithm.digest();
            return messageDigest;
        }
        catch (Exception e)
        {
            log.error("MD5 Error...", e);
        }
        return null;
    }

    private static final String toHex(byte hash[])
    {
        if (hash == null)
        {
            return null;
        }
        StringBuffer buf = new StringBuffer(hash.length * 2);
        int i;

        for (i = 0; i < hash.length; i++)
        {
            if ((hash[i] & 0xff) < 0x10)
            {
                buf.append("0");
            }
            buf.append(Long.toString(hash[i] & 0xff, 16));
        }
        return buf.toString();
    }

    public static String hash(String s)
    {
        try
        {
            return new String(toHex(md5(s)).getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
        }
        catch (Exception e)
        {
            log.error("not supported charset...{}", e);
            return s;
        }
    }

    public static void main(String[] args) {
        final long timestamp = System.currentTimeMillis();
        final String appkey = "PbBUAqof3u";
        final String appuid = "123";
        final String appSecret = "YiKgluDOc9RuAVxHd9iwgqe3OzGWqiXa";
        final String sign = md5New(appSecret + appkey + appuid + timestamp);
        final String token = "hrM65WaLHjvUdJwt3MW6coQi5o2aatyD";
        final StringBuilder apiBuilder = new StringBuilder();
        final String url = "https://openapi.kujiale.com/v2/sso/token";
        apiBuilder.append(url)
                // 鉴权的参数作为URL Query Param
                .append("?appkey=").append(appkey)
                .append("&timestamp=").append(timestamp)
                .append("&sign=").append(sign)
                .append("&appuid=").append(appuid)
                .append("&dest=").append(0);
        JSONObject params = new JSONObject();
        params.put("name", "张三");
        String s = HttpUtils.sendPost(apiBuilder.toString(), params.toJSONString(), "application/json;charset=utf-8");
//        String url = "https://www.kujiale.com/v/auth";
//        url = url + "?appkey=" + appkey + "&timestamp=" + timestamp + "&sign=" + sign + "&accesstoken=" + token + "&dest=0";
//        HttpUtils.sendGet(url);

    }
}
